import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';
import doc_code from '@dfeidao/atom-nodejs/msg/doc-code';
import sys_date from '@dfeidao/atom-nodejs/msg/sys-date';
import { systemid } from '../..//atom/config';

interface Message {
	productid: string;
	tenant_name: string;
	link_man?: string;
	phone?: string;
	email?: string;
	tenant_level: string;
	usercode: string;
	user_name: string;

}
export default async function atom(msg: Message, action_id: string, session_id: string) {

	const time = await sys_date(action_id, session_id);
	const { insert, exec } = operate(action_id, session_id, '12');
	const c = count(action_id, session_id, '12');

	c.prepare('pub_tenant', { productid: msg.productid, tenant_name: msg.tenant_name });
	const [t] = await c.exec();


	// return t;
	if (t > 0) {
		return { status: 'other', tenantid: '' };
	}
	const code = await doc_code(action_id, session_id, '12', 'BH013', 'U', 1);
	const tenantid = code.results[0];

	insert('pub_tenant', { productid: msg.productid, tenantid, tenant_name: msg.tenant_name, link_man: msg.link_man, phone: msg.phone, email: msg.email, tenant_level: msg.tenant_level, is_disable: 'false', create_time: time });
	const res = await exec();


	// 添加操作日志
	// “张三”在[产品类型]页面“新增”了一条编号为“LX0000001”的数据
	const { insert: i1, exec: e1 } = operate(action_id, session_id, systemid);
	// const operate_type = '新增';
	// const op_cont = msg.user_name + '在租户信息管理页面' + operate_type + '了一条编号为' + tenantid + '的数据'; // 操作内容
	// i1('system_log', { pub_user_no: msg.usercode, resume_name: msg.user_name, operate_type, op_cont, operation_time: time, productid: msg.productid });
	i1('operation_log', {
		pub_user_no: msg.usercode,
		resume_name: msg.user_name,
		operate_type: '新增',
		op_cont: `“${msg.user_name}”在租户信息管理页面新增了一条编号为“${tenantid}”的数据`,
		operation_time: time,
		productid: msg.productid
	});
	const ad = await e1();

	if (res.code === 1 && ad.code === 1) {
		return { status: 'success', tenantid };
	} else {
		return { status: 'fail', tenantid };
	}

}
